package com.sunwayworld.basemodule.basedata.index.service.impl;

import com.sunwayworld.framework.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.basedata.index.dao.LimsIndexDao;
import com.sunwayworld.basemodule.basedata.index.bean.LimsIndexBean;
import com.sunwayworld.basemodule.basedata.index.service.LimsIndexService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.List;

@Repository
@GikamBean
public class LimsIndexServiceImpl implements LimsIndexService {

    @Autowired
    private LimsIndexDao limsIndexDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsIndexDao getDao() {
        return limsIndexDao;
    }

    @Override
    @Transactional
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsIndexBean limsIndex = jsonWrapper.parseUnique(LimsIndexBean.class);
        limsIndex.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsIndex);
        return limsIndex.getId();
    }


    @Override
    @Transactional
    public void updateStatus(RestJsonWrapperBean wrapper) {
        String status = wrapper.getParamValue("status");
        if (StringUtils.isEmpty(status)) return;
        List<LimsIndexBean> indexBeanList = wrapper.parse(LimsIndexBean.class);
        indexBeanList.forEach(e -> e.setStatus(status));
        getDao().fastUpdate(indexBeanList, "status");
    }

    @Override
    @Transactional
    public void delete(RestJsonWrapperBean wrapper) {
        List<Long> ids = wrapper.parseId(Long.class);
        getDao().deleteByIdList(ids);
    }
}
